package com.jackaltsc.mydemolist.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jackaltsc.mydemolist.R;
import com.jackaltsc.mydemolist.entity.ClassEntity;
import com.jackaltsc.mydemolist.entity.Student;

import java.util.List;


/**
 * Created by JackalTsc on 2016/7/13.
 */
public class ClassListAdapter extends BaseExpandableListAdapter {

    private List<ClassEntity> mData;
    private LayoutInflater mInflater;

    public ClassListAdapter(Context context, List<ClassEntity> data) {
        mData = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition).getStuList().size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return mData.get(groupPosition).getClassName();
    }

    @Override
    public Student getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).getStuList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View groupConvertView, ViewGroup parent) {

        ViewHolderGroup viewHolderGroup;

        if (groupConvertView == null) {

            groupConvertView = mInflater.inflate(R.layout.view_itemgroup_exlist, null);

            viewHolderGroup = new ViewHolderGroup();
            viewHolderGroup.tvClassName = (TextView) groupConvertView.findViewById(R.id.tv_classname);
            groupConvertView.setTag(viewHolderGroup);

        } else {
            viewHolderGroup = (ViewHolderGroup) groupConvertView.getTag();
        }

        viewHolderGroup.tvClassName.setText(mData.get(groupPosition).getClassName());

        return groupConvertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View childConvertView, ViewGroup parent) {

        ViewHolderChild viewHolderChild;

        if (childConvertView == null) {
            childConvertView = mInflater.inflate(R.layout.view_itemchild_exlist, null);

            viewHolderChild = new ViewHolderChild();
            viewHolderChild.ivStuPhoto = (ImageView) childConvertView.findViewById(R.id.iv_stuphoto);
            viewHolderChild.tvStuName = (TextView) childConvertView.findViewById(R.id.tv_stuname);

            childConvertView.setTag(viewHolderChild);
        } else {
            viewHolderChild = (ViewHolderChild) childConvertView.getTag();
        }

        viewHolderChild.ivStuPhoto.setImageResource(mData.get(groupPosition).getStuList().get(childPosition).getStuPhotoId());
        viewHolderChild.tvStuName.setText(mData.get(groupPosition).getStuList().get(childPosition).getStuName());

        return childConvertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class ViewHolderGroup {
        TextView tvClassName;
    }

    static class ViewHolderChild {
        ImageView ivStuPhoto;
        TextView tvStuName;
    }

}
